package com.jingdianjichi.auth.application.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthRoleConvert;
import com.jingdianjichi.auth.application.dto.AuthRoleDTO;
import com.jingdianjichi.auth.domain.entity.AuthRoleBO;
import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;
import com.jingdianjichi.auth.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;


@RestController
@RequestMapping("/role/")
@Slf4j
public class AuthRoleController {
    @Resource
    private AuthRoleDomainService authRoleDomainService;
    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRole0DTO) {
        // 此处仅作        模拟示例，真实项目需要从数据库中查询数据进行比对
        try {
            if(log.isInfoEnabled()){
                log.info("authRole0DTO:{}", JSON.toJSONString(authRole0DTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRole0DTO.getRoleName()),"角色名不得为空！");
            Preconditions.checkArgument(!StringUtils.isBlank(authRole0DTO.getRoleKey()),"RoleKey不能为空！");
            AuthRoleBO authUserBO= AuthRoleConvert.INSTANCE.authRoleToAuthRoleBO(authRole0DTO);
            Boolean result=authRoleDomainService.add(authUserBO);
            return Result.ok(result);
        }catch (Exception e){
            return  Result.fail("新增失败");
        }

    }

    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("authRoleDTO:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()),"角色名不得为空！");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()),"roleKey不能为空！");
            Preconditions.checkNotNull(authRoleDTO.getId(),"id不能为空！");
            AuthRoleBO authUserBO= AuthRoleConvert.INSTANCE.authRoleToAuthRoleBO(authRoleDTO);
            Boolean result=authRoleDomainService.update(authUserBO);
            return Result.ok(result);
        }catch (Exception e){
            return  Result.fail("更新失败");
        }

    }

    /**
     * 删除用户
     * @param authUserDTO
     * @return
     */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("AuthUserDTO:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"id不得为空");
            AuthRoleBO authUserBO= AuthRoleConvert.INSTANCE.authRoleToAuthRoleBO(authRoleDTO);
            Boolean result=authRoleDomainService.delete(authUserBO);
            return Result.ok(result);
        }catch (Exception e){
            return  Result.fail("删除失败");
        }

    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
}